package com.hyz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyz.pojo.IsComplete;
import com.hyz.pojo.ResponseResult;
import com.hyz.pojo.asset.Asset;
import com.hyz.pojo.asset.AssetClasses;
import com.hyz.pojo.asset.AssetStatus;
import com.hyz.pojo.assetMaintenance.AssetMaintenance;
import com.hyz.pojo.assetMaintenance.AssetMaintenanceResult;
import com.hyz.pojo.assetMaintenance.AssetMaintenanceStatus;
import com.hyz.pojo.assetRetirement.AssetRetirement;
import com.hyz.pojo.assetRetirement.AssetRetirementResult;
import com.hyz.pojo.assetRetirement.AssetRetirementStatus;
import com.hyz.pojo.assetSecondment.AssetSecondment;
import com.hyz.pojo.assetSecondment.AssetSecondmentResult;
import com.hyz.pojo.assetSecondment.AssetSecondmentStatus;
import com.hyz.service.*;
import com.hyz.utils.JudgeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * @author HGD
 * @date 2022/10/8 20:12
 */
@Api("资产借调人员")
@RestController
@RequestMapping("/applicant")
public class ApplicantController {
    @Autowired
    private AssetService assetService;
    @Autowired
    private AssetSecondmentService asService;
    @Autowired
    private AssetMaintenanceService amService;
    @Autowired
    private AssetRetirementService arService;
    @Autowired
    private JudgeUtils judgeUtils;

    //----------------------------------------------------------------
    // 资产借调

    @ApiOperation("资产借调人员获取所有的可借资产")
    @PostMapping("/getAllowBorrowAssets")
    public ResponseResult<List<Asset>> getAllowBorrowAssets() {
        QueryWrapper<Asset> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT asset_name,classes")
                .lambda()
                .eq(Asset::getStatus, AssetStatus.IN_REPOSITORY);
        List<Asset> list = assetService.list(wrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "无可借用资产");
        }
        return new ResponseResult<>(200, "可借资产种类和名称", list);
    }

    @ApiOperation("申请人根据资产种类获取资产列表")
    @PostMapping("/getAllowBorrowAssetsByClasses")
    public ResponseResult<List<Asset>> getAllowBorrowAssetsByClasses(@RequestParam("classes") AssetClasses classes) {
        QueryWrapper<Asset> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT asset_name,classes")
                .lambda()
                .eq(Asset::getStatus, AssetStatus.IN_REPOSITORY)
                .eq(Asset::getClasses, classes);
        List<Asset> list = assetService.list(wrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "无可借用资产");
        }
        return new ResponseResult<>(200, "可借资产种类和名称", list);
    }

    @ApiOperation("借调人员获取借调记录")
    @PostMapping("/getAssetSecondments")
    public ResponseResult<List<AssetSecondment>> getAssetSecondments(@RequestBody Long applicantId) {
        QueryWrapper<AssetSecondment> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AssetSecondment::getApplicantId, applicantId);
        List<AssetSecondment> assetSecondments = asService.list(wrapper);
        if (assetSecondments.isEmpty()) {
            return new ResponseResult<>(400, "无借调记录");
        }
        return new ResponseResult<>(200, "借调记录", assetSecondments);
    }

    @ApiOperation("借调人员创建借调申请")
    @PostMapping("/insertAssetSecondment")
    public ResponseResult<List<AssetSecondment>> insertAssetSecondment(@RequestParam("assetName") String assetName,
                                                                       @RequestParam("classes") AssetClasses classes,
                                                                       @RequestParam("applicantId") Long applicantId,
                                                                       @RequestParam("secondmentReason") String secondmentReason,
                                                                       @RequestParam("status") AssetSecondmentStatus status) {
        QueryWrapper<Asset> assetWrapper = new QueryWrapper<>();
        assetWrapper.lambda().eq(Asset::getAssetName, assetName)
                .eq(Asset::getClasses, classes);
        List<Asset> list = assetService.list(assetWrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "无对应资产");
        }
        assetWrapper.lambda().eq(Asset::getStatus, AssetStatus.IN_REPOSITORY);
        list = assetService.list(assetWrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "无对应的可借资产");
        }
        AssetSecondment assetSecondment = new AssetSecondment();
        assetSecondment.setAssetName(assetName);
        assetSecondment.setAssetClasses(classes);
        assetSecondment.setApplicantId(applicantId);
        assetSecondment.setSecondmentReason(secondmentReason);
        assetSecondment.setStatus(status);
        assetSecondment.setResult(AssetSecondmentResult.CREATE);
        assetSecondment.setIsComplete(IsComplete.UNFINISHED);

        if (asService.save(assetSecondment)) {
            return getAssetSecondments(applicantId);
        }
        return new ResponseResult<>(400, "申请失败，请稍后再尝试");
    }

    //----------------------------------------------------------------
    // 资产维修

    @ApiOperation("申请人获取资产维修申请")
    @PostMapping("apGetAssetMaintenance")
    public ResponseResult<List<AssetMaintenance>> apGetAssetMaintenance(@RequestParam("applicantId") Long applicantId) {
        QueryWrapper<AssetMaintenance> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AssetMaintenance::getApplicantId, applicantId);
        List<AssetMaintenance> list = amService.list(wrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "不存在维修申请");
        }
        return new ResponseResult<>(200, "维修申请列表", list);
    }

    @ApiOperation("申请人创建资产维修")
    @PostMapping("apInsertAssetMaintenance")
    public ResponseResult<Boolean> apInsertAssetMaintenance(@RequestParam("assetId") Long assetId,
                                                            @RequestParam("applicantId") Long applicantId) {
        // 1. 判断资产是否存在
        Asset asset = assetService.getById(assetId);
        if (Objects.isNull(asset)) {
            return new ResponseResult<>(400, "资产不存在", false);
        }
        // 2. 判断资产状态是否正常
        boolean assetStatus = Objects.equals(asset.getStatus(), AssetStatus.IN_REPOSITORY)
                || Objects.equals(asset.getStatus(), AssetStatus.SECONDMENT);
        if (!assetStatus) {
            return new ResponseResult<>(400, "资产状态错误", false);
        }
        // 3. 判断资产是否已存在正在进行的维修申请
        QueryWrapper<AssetMaintenance> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AssetMaintenance::getAssetId, assetId)
                .eq(AssetMaintenance::getIsComplete, IsComplete.UNFINISHED);
        AssetMaintenance am = amService.getOne(wrapper);
        if (Objects.nonNull(am)) {
            return new ResponseResult<>(400, "资产已存在维修申请", false);
        }
        // 4. 判断申请人资格
        if (!judgeUtils.checkApplicant(assetId, applicantId)) {
            return new ResponseResult<>(400, "申请人无资格", false);
        }
        // 5. 创建资产维修申请
        am = new AssetMaintenance();
        am.setAssetId(assetId);
        am.setApplicantId(applicantId);
        am.setResult(AssetMaintenanceResult.CREATE);
        am.setIsComplete(IsComplete.UNFINISHED);
        if (Objects.equals(asset.getStatus(), AssetStatus.IN_REPOSITORY)) {
            am.setStatus(AssetMaintenanceStatus.IN_REPOSITORY);
        } else if (Objects.equals(asset.getStatus(), AssetStatus.SECONDMENT)) {
            am.setStatus(AssetMaintenanceStatus.SECONDMENT);
        } else {
            return new ResponseResult<>(400, "资产不正确", false);
        }
        // 6. 资产状态修改
        asset.setStatus(AssetStatus.MAINTENANCE);
        if (amService.save(am) && assetService.saveOrUpdate(asset)) {
            return new ResponseResult<>(200, "创建资产维修申请成功", true);
        }
        return new ResponseResult<>(400, "创建资产维修申请失败", false);
    }

    //----------------------------------------------------------------
    // 资产报废

    @ApiOperation("申请人获取资产报废申请")
    @PostMapping("apGetAssetRetirement")
    public ResponseResult<List<AssetRetirement>> apGetAssetRetirement(@RequestParam("applicantId") Long applicantId) {
        // 1. 判断用户是否有对应的资产报废申请
        QueryWrapper<AssetRetirement> arWrapper = new QueryWrapper<>();
        arWrapper.lambda().eq(AssetRetirement::getApplicantId, applicantId);
        List<AssetRetirement> list = arService.list(arWrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "无资产报废申请");
        }
        return new ResponseResult<>(200, "资产报废申请", list);
    }

    @ApiOperation("申请人创建资产报废申请")
    @PostMapping("apInsertAssetRetirement")
    public ResponseResult<Boolean> apInsertAssetRetirement(@RequestParam("assetId") Long assetId,
                                                           @RequestParam("applicantId") Long applicantId) {
        // 1. 判断资产是否存在
        Asset asset = assetService.getById(assetId);
        if (Objects.isNull(asset)) {
            return new ResponseResult<>(400, "资产不存在", false);
        }
        // 2. 判断资产状态是否为上锁
        if (Objects.equals(asset.getStatus(), AssetStatus.LOCK)) {
            return new ResponseResult<>(400, "资产不可操作", false);
        }
        // 3. 判断该资产是否存在未完成的资产报废申请
        QueryWrapper<AssetRetirement> arWrapper = new QueryWrapper<>();
        arWrapper.lambda().eq(AssetRetirement::getAssetId, assetId)
                .eq(AssetRetirement::getIsComplete, IsComplete.UNFINISHED);
        if (Objects.nonNull(arService.getOne(arWrapper))) {
            return new ResponseResult<>(400, "资产已存在报废申请", false);
        }
        // 4. 判断申请人资格
        if (!judgeUtils.checkApplicant(assetId, applicantId)) {
            return new ResponseResult<>(400, "申请人无资格", false);
        }
        // 5. 创建资产报废申请
        AssetRetirement ar = new AssetRetirement();
        ar.setApplicantId(applicantId);
        ar.setAssetId(assetId);
        ar.setResult(AssetRetirementResult.CREATE);
        ar.setIsComplete(IsComplete.UNFINISHED);
        if (Objects.equals(asset.getStatus(), AssetStatus.IN_REPOSITORY)) {
            ar.setStatus(AssetRetirementStatus.IN_REPOSITORY);
        } else if (Objects.equals(asset.getStatus(), AssetStatus.SECONDMENT)) {
            ar.setStatus(AssetRetirementStatus.SECONDMENT);
        } else {
            return new ResponseResult<>(400, "资产不正确", false);
        }
        // 6. 修改资产状态
        asset.setStatus(AssetStatus.RETIRED);
        if (arService.save(ar) && assetService.saveOrUpdate(asset)) {
            return new ResponseResult<>(200, "资产报废创建成功", true);
        }
        return new ResponseResult<>(400, "资产报废创建失败", false);
    }

}
